package com.zhgxi.security.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhgxi.core.orm.PageData;
import com.zhgxi.core.utils.WebUtils;
import com.zhgxi.security.entity.Resources;
import com.zhgxi.security.entity.Roles;
import com.zhgxi.security.service.ResourcesService;
import com.zhgxi.security.service.RolesService;

@Controller
@RequestMapping("/security/roles")
public class RolesAction
{
	@Resource
	private RolesService rolesService;
	
	@Resource
	private ResourcesService resourcesService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request)
	{
		PageData<Roles> pageData = new PageData<Roles>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = rolesService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model)
	{
		List<Resources> resourcesList = resourcesService.findAll();
		model.addAttribute("resourcesList",resourcesList);
		return "/security/roles/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model)
	{
		Roles roles = rolesService.find(id);
		model.addAttribute(roles);
		List<Resources> resourcesList = resourcesService.findAll();
		model.addAttribute("resourcesList",resourcesList);
		return "/security/roles/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Roles roles)
	{
		if (roles.isNew())
		{
			rolesService.save(roles);
		} else
		{
			rolesService.update(roles);
		}

		return "redirect:/security/roles/list";//重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id)
	{
		rolesService.delete(id);
		return "redirect:/security/roles/list";//重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id)
	{
		rolesService.visible(id);
		return "redirect:/security/roles/list";//重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id)
	{
		rolesService.unVisible(id);
		return "redirect:/security/roles/list";//重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder)
	{
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
